<?php


namespace app\common\lib\live;


use app\chat\LiveMessage;
use app\common\cachemodel\live\LiveRtcRecord;
use think\Cache;
use think\queue\Job;

class LiveRtcJob
{
    /**
     * 连麦超时关闭
     * @param Job $job
     * @param $jobData
     * @throws \Exception
     */
    public function clientTimeoutCheckTask(Job $job, $jobData)
    {
        $rtcRecordModel = new LiveRtcRecord();
        // 获取当前 直播间 连麦信息
        $nowRtcData = $rtcRecordModel->getNowRtcDataByCache($jobData['live_record_id']);

        if ($nowRtcData && $nowRtcData['rtc_sign'] === $jobData['rtc_sign'] && (int)$nowRtcData['status'] === 1)
        {   // 用户长时间 未响应 系统自动取消连麦
            $time = time();
            if ($nowRtcData['id'] > 0)
            {
                $questionRtcInfo = $rtcRecordModel->getInfoById($nowRtcData['id']);
                // 更新连麦状态
                $rtcRecordModel->updateRtcInfo($nowRtcData['id'], ['status' => 15, 'cancel' => $time, 'update_time' => $time]);
                $rtcRecordModel->rtcListDel($jobData['live_record_id'], (int)$questionRtcInfo['uid']);    // 删除从列表中移除
            }

            $rtcRecordModel->delNowRtcDataByCache($jobData['live_record_id']);   // 删除当前连麦信息
            // 拼接 发送给管理端 数据
            $popMsgRtc2All = [
                'type' => 0,
                'toast' => '',
                'user_info' => null,
                'btn_list' => []
            ];

            // 拼接 发送给管理端 数据
            $popMsgRtc2RtcUser = [
                'type' => 2,
                'toast' => '已超时，自动关闭连麦',
                'user_info' => null,
                'btn_list' => []
            ];

            // 拼接 发送给管理端 数据
            $popMsgRtc2Admin = [
                'type' => 2,
                'toast' => '已超时，自动关闭连麦',
                'user_info' => null,
                'btn_list' => []
            ];

            $userClientId = LiveMessage::getClientIdByUid($nowRtcData['uid']);
            // 向直播间所有人 发送消息
            LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_APP_USER, $nowRtcData['live_id'], $nowRtcData['live_id'])
                , LiveMessage::MSG_TYPE_POP_MSG_RTC, $popMsgRtc2All, $userClientId);
            // 向连麦用户发送消息
            LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_UID, $nowRtcData['live_id'], $nowRtcData['uid'])
                , LiveMessage::MSG_TYPE_POP_MSG_RTC, $popMsgRtc2RtcUser);
            // 向管理员 发送消息
            LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_LIVER, $nowRtcData['live_id'], $nowRtcData['live_id'])
                , LiveMessage::MSG_TYPE_POP_MSG_RTC, $popMsgRtc2Admin);

            // 获取当前 rtc 开启状态
            $rtcSwitch = (int)(Cache::get("live:rtc_switch:{$nowRtcData['live_id']}:{$jobData['live_record_id']}", 0));
            $allRequestRtcUserList = $rtcRecordModel->getRtcUserList($jobData['live_record_id']);
            if ($allRequestRtcUserList)
            {
                $adminRtcStatus = 3;
            } else
            {
                $adminRtcStatus = $rtcSwitch;
            }

            // 向连麦用户发送消息
            LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_UID, $nowRtcData['live_id'], $nowRtcData['uid'])
                , LiveMessage::MSG_TYPE_RTC_STATUS, $rtcSwitch);
            // 向直播间 管理员和主播 发送连麦 状态消息
            LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_LIVER, $nowRtcData['live_id'], $nowRtcData['live_id'])
                , LiveMessage::MSG_TYPE_RTC_STATUS, $adminRtcStatus);
        }

        // 删除队列
        $job->delete();
    }
}
